package edu.ccut.saturn.studio.core.util.eclipse;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.saturnproperty.StringUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

public final class SaturnEclipseClassPathUtil {
	
	private static final String CLASSPATH_FILENAME = StringUtils.FILE_SEPARATOR+"classpath";
	private static final String CLASSPATH_ATTRIBUTE_CLASSPATHENTRY = "classpathentry";
	private static final String CLASSPATH_ATTRIBUTE_COMBINEACCESSRULES = "combineaccessrules";
	private static final String CLASSPATH_ATTRIBUTE_KIND = "kind";
	private static final String CLASSPATH_ATTRIBUTE_PATH= "path";
	public static final String CLASSPATH_ATTRIBUTE_SRC = "src";
	
	
	/**
	 * 通过一个工程对象，获得此工程对象的CLASSPATH文件，解析出ARG为参数的类型文件夹
	 * @param project
	 * @param arg
	 * @return
	 */
	public static final List<String> getClassPahtInfo(IProject project,String arg){
		List<String> pathvalues = new ArrayList<String>();
		String projectPath = project.getLocation().toOSString();
		StringBuffer classBuffer = new StringBuffer(projectPath);
		classBuffer.append(StringUtils.DIRECOTRY_SEPARATOR).append(CLASSPATH_FILENAME);
		String classPath = classBuffer.toString();
		Document doc = null;
		try {
			doc = SaturnW3CUtil.getDocument(classPath);
		} catch (XMLException e) {
			e.printStackTrace();
		}
		if (doc != null) {
			Element rootElement = doc.getDocumentElement();
			NodeList classpathentrynodes = rootElement
					.getElementsByTagName(CLASSPATH_ATTRIBUTE_CLASSPATHENTRY);
			for (int i = 0; i < classpathentrynodes.getLength(); i++) {
				String combineaccessrules = SaturnW3CUtil.getNodeAttributeValue(
						classpathentrynodes.item(i), CLASSPATH_ATTRIBUTE_COMBINEACCESSRULES);
				if (!combineaccessrules.equals("")) {
					continue;
				}
				String kindvalue = SaturnW3CUtil.getNodeAttributeValue(
						classpathentrynodes.item(i), CLASSPATH_ATTRIBUTE_KIND);
				if (kindvalue.equals(arg)) {
					String pathvalue = SaturnW3CUtil.getNodeAttributeValue(
							classpathentrynodes.item(i), CLASSPATH_ATTRIBUTE_PATH);
					pathvalues.add(pathvalue);
				}
			}

		}
		return pathvalues;
	}
}
